package com.yy.device.boot.protocol;

import com.google.common.collect.Lists;
import com.yy.device.constant.DeviceConstant;
import com.yy.device.enums.DeviceFunctionEnum;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 上报消息
 *
 * @author zhiqiang.sun
 * @date 2022/06/21
 */
@Slf4j
@Data
@EqualsAndHashCode(callSuper = true)
public class ReportMessage extends Message {
    //74-15 -5 =54
    private byte head;
    private byte version;
    private short length;
    private long taskId;

    private byte address;
    private DeviceFunctionEnum deviceFunctionEnum;
    private byte lengthMod;


    private List<Short> data = Lists.newArrayListWithCapacity(DeviceConstant.DATA_LENGTH);

    private short crcMod;
    private short crc;
    private byte end;

    @Override
    public void decode(ByteBuf byteBuf) {
        this.setTaskId(byteBuf.readLongLE());
        this.setAddress(byteBuf.readByte());
        this.setDeviceFunctionEnum(DeviceFunctionEnum.getEnumByType(byteBuf.readByte() & 0xff));
        this.setLengthMod(byteBuf.readByte());
        for (int i = 0; i < DeviceConstant.DATA_LENGTH; i++) {
            this.getData().add(byteBuf.readShort());
        }
        this.setCrcMod(byteBuf.readShortLE());
        this.setCrc(byteBuf.readShortLE());
    }
}
